import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {User} from "../../providers/user/user";
import {Api} from "../../providers/api/api";
import {TranslateService} from "@ngx-translate/core";
import {MainPage} from "../pages";

/**
 * Generated class for the ForgetPwdPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-forget-pwd',
  templateUrl: 'forget-pwd.html',
})
export class ForgetPwdPage {

  user: any = {
    mobile: '',
    code: '',
    password: '',
    rePassword: ''
  };

  _user: any;

  isHidden: boolean = false;

  codeValue = '获取验证码';

  status: boolean = false;

  // Our translated text strings
  private networkErrorString: string;

  constructor(public navCtrl: NavController,
              public User: User,
              public api: Api,
              public toastCtrl: ToastController,
              public translateService: TranslateService,
              public navParams: NavParams) {
    this.translateService.get('NETWORK_ERROR').subscribe((value) => {
      this.networkErrorString = value;
    });
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad SmsLoginPage');
  }

  getCode() {
    if (this.user.mobile.length != 11) {
      this.showToast("手机号码格式不正确！");
      return;
    }

    let reqParam = "?mobile=" + this.user.mobile;
    let seq = this.api.get('customers/checkCustomer' + reqParam).share();

    seq.subscribe((res: any) => {
      console.log(res);
      // If the API returned a successful response, mark the user as logged in
      this.status = true;
      let timer = 60;
      let num = setInterval(() => {
        console.log(this.status, this.codeValue);
        if (timer == 0) {
          this.codeValue = '获取验证码';
          this.status = false;
          clearInterval(num);
          return;
        }
        timer--;
        this.codeValue = timer + "s后可重发";
      }, 1000)
    }, err => {
      let msg = err.status == 500 ? '该手机号对应的账号不存在' : this.networkErrorString;
      this.showToast(msg);
    });
  }

  next() {
    if (this.user.mobile.length != 11 || this.user.code.length != 4) {
      this.showToast("请填写正确的手机号和验证码");
      return;
    }
    this.User.smsLogin(this.user.mobile, this.user.code).subscribe((res) => {
      this._user = res;
      this.isHidden = true;
    }, (err) => {
      let msg = err.status == 500 ? '验证码错误' : this.networkErrorString;
      // Unable to log in
      this.showToast(msg);
    });
  }

  changePassword() {
    if (this.user.password.length < 6 || this.user.password != this.user.rePassword) {
      this.showToast("密码长度至少6位，前后两次输入密码必须一致");
      return;
    }
    this.User.changePassword(this.user);
    this.navCtrl.push(MainPage);
  }

  showToast(msg) {
    return this.toastCtrl.create({
      message: msg,
      duration: 2000,
      position: 'top'
    }).present();
  }

}
